﻿using Microsoft.Extensions.Configuration;

namespace Fvs_Service.Log
{
    /// <summary>
    /// 日志
    /// </summary>
    public static class LogPool
    {
        /// <summary>
        /// 构造函数
        /// </summary>
        static LogPool()
        {
            var _config = new ConfigurationBuilder()
                .AddJsonFile(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "appsettings.json"))
                .Build();

            Directory = _config.GetValue("Log:Directory", Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "../log/"));
            HoldDays = _config.GetValue("Log:HoldDays", 0);

            _loggerProviders = new List<Logger>
            {
                new ConsoleLogger(LogLevel.Debug, LogLevel.Error),
                new FileLogger(LogLevel.Debug, LogLevel.Debug, "Debug", Directory, HoldDays),
                new FileLogger(LogLevel.Information, LogLevel.Information, "Info", Directory, HoldDays),
                new FileLogger(LogLevel.Warning, LogLevel.Error, "Error", Directory, HoldDays)
            };
        }

        /// <summary>
        /// 文件日志保存目录
        /// </summary>
        public static string Directory { get; }

        /// <summary>
        /// 文件日志保存天数
        /// </summary>
        public static int HoldDays { get; }

        /// <summary>
        /// 日志提供者集合
        /// </summary>
        private static readonly List<Logger> _loggerProviders;

        public static void LogDebug(int eventId, string message)
        {
            foreach (Logger logger in _loggerProviders)
            {
                logger.Log(LogLevel.Debug, eventId, message, null, null);
            }
        }

        public static void LogInformation(int eventId, string message)
        {
            foreach (Logger logger in _loggerProviders)
            {
                logger.Log(LogLevel.Information, eventId, message, null, null);
            }
        }

        public static void LogWarning(int eventId, Exception ex, string message)
        {
            foreach (Logger logger in _loggerProviders)
            {
                logger.Log(LogLevel.Warning, eventId, message, ex, null);
            }
        }

        public static void LogError(int eventId, Exception ex, string message)
        {
            foreach (Logger logger in _loggerProviders)
            {
                logger.Log(LogLevel.Error, eventId, message, ex, null);
            }
        }

    }
}
